/**
 * 日期工具类
 */

/**
 * 补零
 * @param {*} num
 */
function zeroize(num) {
  return (String(num).length == 1 ? '0' : '') + num
}

function newDate(date = new Date()) {
  if (Number(date)) {
    return (new Date(Number(date)))
  } else {
    // 在ios上必须要用 YYYY/MM/DD 的格式
    // date = date.replace(new RegExp(/-/gm) ,"/");
    return (new Date(date))
  }
}

// 转换为年月日
function formatDateYMD(date = new Date(), splitStr = '-') {
  date = newDate(date)
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  return year + splitStr + zeroize(month) + splitStr + zeroize(day)
}

// 转换为时分秒
function formatDateHMS(date = new Date(), splitStr = ':') {
  date = newDate(date)
  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()
  return zeroize(hour) + splitStr + zeroize(minute) + splitStr + zeroize(second)
}
function formatDateYMDHMS(date, split1, split2) {
  return formatDateYMD(date, split1) + ' ' + formatDateHMS(date, split2)
}


function formatDateWork(date = newDate(), prefix = '星期') {
  const works = ['日', '一', '二', '三', '四', '五', '六']
  return prefix + works[newDate(date).getDay()]
}

module.exports = {
  zeroize,
  newDate,
  formatDateYMD,
  formatDateYMDHMS,
  formatDateWork
}